define(function(require, exports, module) {
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回

	var getFunList = {
		"parmsNO": 0, //参数为1  无计税方式	无折扣	
		"parmsFirst": 1, //参数为1  无计税方式	有折扣	
		"parmsTwo": 2, //参数为2  有计税方式	有折扣
		"parmsThree": 3, //参数为3  不含税价
		"dataCached": [],
		"SortList": function(templateId, mainId) { //产品分类信息列表获取函数
			var option = {};
			var dataModel = {
				"id": 0
			};
			abp.services.app.infoGoodsCatalog.getList(dataModel, option).
			done(function(data) {
				var data1 = {};
				data1.list = data;
				var html = template(templateId, data1);
				$(mainId).append(html);

			}).fail(function(data) {

			});
		},
		"getProductList": function(templateId, mainId, catalogId, isBom, orderBy) { //产品信息列表获取函数
			var option = {};
			var dataModelPro = {
				"keyword": "", //(string, optional): 关键字 ,
				"catalogId": catalogId, //(integer, optional): 货品分类Id ,
				"orderBy": orderBy, //(string, optional): 排序字段 ,
				"whId": 0, //(integer, optional): 仓库id ,
				"pageIndex": 1, //(integer, optional),
				"isBom": isBom,
				"pageSize": 15 //(integer, optional): 分页数；最大分页支持100
			};
			abp.services.app.infoGoods.getPage(dataModelPro, option)
				.done(function(data) {
					console.log(data);
					getFunList.dataCached = [];
					for(var i = 0; i < data.items.length; i++) {
						getFunList.dataCached[data.items[i].id] = data.items[i];
					}

					if(!data.totalCount) {
						$(".mui-content").addClass("searchContentTips");
					} else {
						$(".mui-content").removeClass("searchContentTips");
					}
					var productList = data.items;

					if(data.totalCount <= 0) {
						$(".mui-content").addClass("initContentTips");
					} else {
						$(".mui-content").removeClass("initContentTips");
					}
					var listHtml = template(templateId, data);

					$(mainId).html(listHtml);
				})
				.fail(function(data) {

				});
		},
		"addGoodsInfoList": function(templateId, mainId) { //新增订单选择货品列表
			if(sessionStorage.goodsInfoList) { //定义货品信息列表本地变量
				$("#lastInfo").show();
				$("#goodsListShow").show();
				//					console.log(sessionStorage.goodsInfoList);
				var goodsInfoList = eval(sessionStorage.goodsInfoList);
				var arr = [];
				var obj = {};
				for(var i = 0; i < goodsInfoList.length; i++) {
					var goodsInfo = JSON.parse(goodsInfoList[i]);
					//console.log(goodsInfo);
					arr.push(goodsInfo);
				}
				obj.list = arr;

				var listHtml = template(templateId, obj);
				//alert(listHtml);
				$(mainId).prepend(listHtml);

			} else {
				$("#lastInfo").hide();
				$("#goodsListShow").hide();
			}
		},
		"setParm": function() { //税率和折扣   权限控制
			var parm = sessionStorage.setParm;
			var productSelectSource = sessionStorage.getItem("productSelectSource");
			if(productSelectSource && productSelectSource == "allot") {
				$("#product-Price").css("display", "none");
			}
			//sessionStorage.removeItem("setParm");
			if(parm == 1 || parm == 2) {
				$(".cut").show();
				$(".tax").hide();
			}

			if(parm == 3) {
				$(".cut").show();
				$(".tax").show();
			}

			if(!parm) {
				$(".cut").hide();
				$(".tax").hide();
			}

		},
		"btnShowAndHide": function() { //按钮隐藏判断
			if((sessionStorage.chioceStaffId &&
					sessionStorage.goodsInfoList) ||
				sessionStorage.orderDetail
			) {
				$("#sub-btn").show();
			} else {
				$("#sub-btn").hide();
			}
		},
		"clearSessionStroage": function() { //新增订单页面提交 清除本地存储
			sessionStorage.removeItem("goodsInfoList"); //货品列表信息
			sessionStorage.removeItem("chioceStaff"); //业务员信息
			sessionStorage.removeItem("chioceStaffId");
			sessionStorage.removeItem("chioceSupplierName"); //供应商信息	
			sessionStorage.removeItem("chioceSupplierNameId");
			sessionStorage.removeItem("chioceClientName"); //客户信息	
			sessionStorage.removeItem("chioceClientNameId");
			sessionStorage.removeItem("flag");
			sessionStorage.removeItem("money");
		},
		"btnShow": function() { //按钮隐藏判断
			if(sessionStorage.chioceStaffId &&
				sessionStorage.goodsInfoList &&
				(sessionStorage.chioceSupplierNameId ||
					sessionStorage.chioceClientNameId)
			) {
				$("#sub-btn").show();
			} else {
				$("#sub-btn").hide();
			}
		},
		"choiceInfo": function() {
			if(sessionStorage.chioceSupplierName && sessionStorage.chioceSupplierNameId) { //设置选择供应商
				$("#Supplier").attr("data-id", sessionStorage.chioceSupplierNameId);
				$("#Supplier").text(sessionStorage.chioceSupplierName);
			} else {
				$("#Supplier").text("(请选择)");
				$("#Supplier").attr("data-id", "");
			}
			if(sessionStorage.valoremName && sessionStorage.valoremNameId) { //设置选择计税方式
				$("#valorem").attr("data-id", sessionStorage.valoremNameId);
				$("#valorem").text(sessionStorage.valoremName);
			}
			if(sessionStorage.chioceStaff && sessionStorage.chioceStaffId) { //设置选择采购员
				$("#Staff").attr("data-id", sessionStorage.chioceStaffId);
				$("#Staff").text(sessionStorage.chioceStaff);
			} else {
				$("#Staff").text("(必填)");
				$("#Staff").attr("data-id", "");
			}
			if(sessionStorage.chioceClientName && sessionStorage.chioceClientNameId) { //选择客户
				$("#client").text(sessionStorage.chioceClientName);
				$("#client").attr("data-id", sessionStorage.chioceClientNameId);
			} else {
				$("#client").text("(请选择)");
				$("#client").attr("data-id", "");
			}
		},
		"newInstockCode": function() { //新增入库单号自动生成    客户 业务员自动生成
			if(sessionStorage.chioceStaff && sessionStorage.chioceStaffId) {
				$("#buyer").text(sessionStorage.chioceStaff);
			} else {
				$("#buyer").text("(必填)");
			}
			if(sessionStorage.chioceSupplierName && sessionStorage.chioceSupplierNameId) {
				$("#supplier").text(sessionStorage.chioceSupplierName);
			} else {
				$("#supplier").text("(请选择)");
			}
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.WH_INPUT
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					//console.log(data.serialNo);
					$("#code").html(data.serialNo);
				})
				.fail(function(data) {

				});
		},
		"newOutstockCode": function() {
			if(sessionStorage.chioceStaff && sessionStorage.chioceStaffId) {
				$("#buyer").text(sessionStorage.chioceStaff);
				$("#buyer").attr("data-id", sessionStorage.chioceStaffId);
			} else {
				$("#buyer").text("(必填)");
			}

			if(sessionStorage.chioceClientName && sessionStorage.chioceClientNameId) {
				$("#supplier").text(sessionStorage.chioceClientName);
				$("#supplier").attr("data-id", sessionStorage.chioceClientNameId);
			} else {
				$("#supplier").text("(请选择)");
			}
			var optionSource = {};
			var source = {
				"sourceType": abp.sourceType.WH_OUTPUT
			};
			abp.services.app.sysClientSettingNumber.getSerialNo(source, optionSource)
				.done(function(data) {
					//console.log(data.serialNo);
					$("#code").text(data.serialNo);
				})
				.fail(function(data) {

				});
		},
		"changeOrderDetail": function(templateId, mainId) { //编辑订单 渲染页面
			$("#lastInfo").show();
			$("#goodsListShow").show();
			$("#sub-btn").show();
			if(sessionStorage.orderDetail) {
				var goodsInfoList = [];
				var orderDetail = JSON.parse(sessionStorage.orderDetail);
				sessionStorage.setItem("orderInDetail", JSON.stringify(orderDetail));
				console.log(orderDetail);
				$("#code").text(orderDetail.serialNo); //单号
				$("#code").attr("data-id", orderDetail.id); //单号
				$("#createDate").text(orderDetail.dataCreated); //单号日期
				$("#supplier").attr("data-id", orderDetail.companyId); //客户ID			//
				if(!Number(orderDetail.companyId)) {
					$("#supplier").text("(请选择)"); //客户	
				} else {
					$("#supplier").text(orderDetail.companyName); //客户	
				}
				$("#buyer").text(orderDetail.busUserName); //业务员
				$("#buyer").attr("data-id", orderDetail.busUserId); //业务员ID

				var orderInfo = {
					"id": orderDetail.id,
					"serialNo": orderDetail.serialNo,
					"dataCreated": orderDetail.dataCreated,
					"companyId": orderDetail.companyId,
					"companyName": orderDetail.companyName,
					"busUserName": orderDetail.busUserName,
					"busUserId": orderDetail.busUserId
				};
				sessionStorage.orderInfo = JSON.stringify(orderInfo);

				if(sourceType == 5004) {
					$("#mui-choice").hide();
				}
				if(outSourceType == 5004) {
					$("#mui-choice").hide();
				}
				//入库来源sourceType标识
				//出库outSourceType标识
				var sourceType, outSourceType;
				if(orderDetail.whInputDetail) {
					var orderGoodsList = orderDetail.whInputDetail;
					console.log(orderGoodsList);
					//						var sourceType = orderDetail.whOutputDetail[0].sourceType;
					sourceType = orderGoodsList[0].sourceType; //来源类型

					for(var i = 0; i < orderGoodsList.length; i++) {
						try {
							var goodsImg = orderGoodsList[i].infoGoodsImg[0].imgPath || "../../img/chioceProduct/product-icon.png";
						} catch(e) {

						}
						var oneGoods = {
							"goodsSerial": orderGoodsList[i].goodsSerial,
							"goodsId": orderGoodsList[i].goodsId,
							"goodsName": orderGoodsList[i].goodsName,
							"goodsBuyNum": orderGoodsList[i].number,
							"goodsPrice": orderGoodsList[i].price,
							"goodsStock": orderGoodsList[i].number, //库存
							"goodsWhid": orderGoodsList[i].whId,
							"goodsWhName": orderGoodsList[i].whName,
							"goodsUnit": orderGoodsList[i].unit,
							"goodsImg": goodsImg,
							"notes": orderGoodsList[i].notes
						};
						goodsInfoList.push(JSON.stringify(oneGoods));
						sessionStorage.goodsInfoList = JSON.stringify(goodsInfoList);
					}
				}
				if(orderDetail.whOutputDetail) {
					var orderGoodsList = orderDetail.whOutputDetail;
					outSourceType = orderGoodsList[0].sourceType;

					for(var i = 0; i < orderGoodsList.length; i++) {
						try {
							var goodsImg = orderGoodsList[i].infoGoodsImg[0].imgPath || "../../img/chioceProduct/product-icon.png";
						} catch(e) {

						}
						var oneGoods = {
							"goodsSerial": orderGoodsList[i].goodsSerial,
							"goodsId": orderGoodsList[i].goodsId,
							"goodsName": orderGoodsList[i].goodsName,
							"goodsBuyNum": orderGoodsList[i].number,
							"goodsPrice": orderGoodsList[i].price,
							"goodsStock": orderGoodsList[i].number, //库存
							"goodsWhid": orderGoodsList[i].whId,
							"goodsWhName": orderGoodsList[i].whName,
							"goodsUnit": orderGoodsList[i].unit,
							"goodsImg": goodsImg,
							"notes": orderGoodsList[i].notes
						};
						goodsInfoList.push(JSON.stringify(oneGoods));
						sessionStorage.goodsInfoList = JSON.stringify(goodsInfoList);
					}

				}
				sessionStorage.setItem("sourceType", sourceType); //入库存储来源类型
				sessionStorage.setItem("outSourceType", outSourceType); //出库存储来源类型

				sessionStorage.removeItem("orderDetail");
			}
			var goodsInfoList = eval(sessionStorage.goodsInfoList);
			console.log(goodsInfoList);
			var arr = [];
			var obj = {};
			for(var i = 0; i < goodsInfoList.length; i++) {
				var goodsInfo = JSON.parse(goodsInfoList[i]);
				arr.push(goodsInfo);
			}
			obj.list = arr;

			var listHtml = template(templateId, obj);
			//alert(listHtml);
			$(mainId).prepend(listHtml);

			if(sessionStorage.orderInfo) {
				var orderInfo = JSON.parse(sessionStorage.orderInfo);
				$("#supplier").attr("data-id", orderInfo.companyId); //客户ID			//
				if(!Number(orderInfo.companyId)) {
					$("#supplier").text("(请选择)"); //客户	
				} else {
					$("#supplier").text(orderInfo.companyName); //客户	
				}
				$("#buyer").text(orderInfo.busUserName); //业务员
				$("#buyer").attr("data-id", orderInfo.busUserId); //业务员ID
			}

			if(sessionStorage.chioceClientName && sessionStorage.chioceClientNameId) {
				$("#supplier").text(sessionStorage.chioceClientName); //客户			//
				$("#supplier").attr("data-id", sessionStorage.chioceClientNameId); //客户ID			//
			}
			if(sessionStorage.chioceSupplierName && sessionStorage.chioceSupplierNameId) {
				$("#supplier").text(sessionStorage.chioceSupplierName); //客户			//
				$("#supplier").attr("data-id", sessionStorage.chioceSupplierNameId); //客户ID			//
			}

			if(sessionStorage.chioceStaff && sessionStorage.chioceStaffId) {
				$("#buyer").text(sessionStorage.chioceStaff); //业务员
				$("#buyer").attr("data-id", sessionStorage.chioceStaffId); //业务员ID
			}

			if(sessionStorage.orderInfo) {
				var orderInfo = JSON.parse(sessionStorage.orderInfo);
				//		    		console.log(orderInfo);
				$("#code").text(orderInfo.serialNo); //单号
				$("#code").attr("data-id", orderInfo.id);
				$("#createDate").text(orderInfo.dataCreated); //单号日期
			}

		},
		"settlement": function() { //计算价格函数
			var price = 0;
			var allNum = 0;
			var goodsNum = 0;
			var allPrice = 0;
			$.each($('li.production-list'), function(i, v) {
				price = Number($(v).find('.price').text());
				goodsNum = Number($(v).find('.goodsNum').text());
				allNum += Number($(v).find('.goodsNum').text());
				allPrice += (price * goodsNum);
			});
			$('#allNum').text(allNum);
			$('#allPrice').text(allPrice);
		},
		"infoFromOrderShow": function() { //编辑订单 货品信息编辑 详情页面展示
			if(sessionStorage.goodsInfo) {
				var goodsInfo = JSON.parse(sessionStorage.goodsInfo);
				//					sessionStorage.removeItem("goodsInfo");
				console.log(goodsInfo);
				$("#goodsSerial").text(goodsInfo.goodsSerial);
				$("#goodsName").text(goodsInfo.goodsName);
				$("#goodsName").attr("data-unit", goodsInfo.goodsUnit);
				$("#goodsName").attr("data-id", goodsInfo.goodsId);
				try {
					var goodsImg = goodsInfo.goodsImg || "../../img/chioceProduct/product-icon.png";
				} catch(e) {

				}
				$(".product-img").attr("src", goodsImg);
				if(sessionStorage.libraryName && sessionStorage.libraryNameId) {
					$("#whid").text(sessionStorage.libraryName);
					$("#whid").attr("data-id", sessionStorage.libraryNameId);
				} else {
					$("#whid").text(goodsInfo.whName);
					$("#whid").attr("data-id", goodsInfo.whId);
				}
				$("#goodsNum").val(goodsInfo.number);
				$("#goodsPrice").val(goodsInfo.price);
				$("#fromInstockMoney").text((Number(goodsInfo.number) * Number(goodsInfo.price)).toFixed(2));
				sessionStorage.goodsName = goodsInfo.goodsName;
				sessionStorage.goodsSerial = goodsInfo.goodsSerial;
				sessionStorage.goodsId = goodsInfo.goodsId;
				sessionStorage.goodsUnit = goodsInfo.unit;
			}
		},
		"keepOrderInfo": function() { //保存编辑订单的单号与日期
			var code = $("#code").text();
			var createDate = $(".createDate").text();
			var lastDate = $(".lastDate").text();
			var supplierID = $("#supplier").attr("data-id");
			var supplierName = $("#supplier").text();
			var buyerID = $("#supplier").attr("data-id");
			var buyerName = $("#supplier").text();
			var orderInfo = {
				"code": code,
				"createDate": createDate,
				"lastDate": lastDate,
				"supplierID": supplierID,
				"supplierName": supplierName,
				"buyerID": buyerID,
				"buyerName": buyerName
			};
			sessionStorage.orderInfo = JSON.stringify(orderInfo);
		},
		"getGoodsWhInvneotory": function() { //根据相应货品id 仓库id查看库存量
			var id = $("#goodsName").attr("data-id");
			var outLibraryId = sessionStorage.outLibraryId;
			var whId = $("#whid").attr("data-id");
			if(outLibraryId && typeof(outLibraryId) != 'undefined') {
				whId = outLibraryId;
			}
			if(whId) {
				var option = {};
				var dataModel = {
					"id": Number(id),
					"whId": Number(whId)
				};
				abp.services.app.whInventoryTotal.getGoodsWhInvneotory(dataModel, option)
					.done(function(data) {
						$("#goodsStock").text(data.totalNumber);
					})
					.fail(function(data) {});
			}
		}
	};

	module.exports = getFunList;
});